package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.shiro.MyRealm;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {
    @Autowired
    private CustomerTransferMapper CustomerTransferMapper;
    @Autowired
    private CustomerMapper  customerMapper;
    @Autowired
    private MyRealm myRealm;

    @Override
    public void saveOrUpdate(CustomerTransfer entity) {
            //0 如果当前用户没有提交newSellerid 说明是吸纳
            if(entity.getNewSeller()==null)
            {
                entity.setNewSeller(UserContext.getCurrentEmp());
            }

            //1封装历史的操作人
            entity.setOperator(UserContext.getCurrentEmp());
            CustomerTransferMapper.insert(entity);
            //根据客户id修改当前的sellerId
            //更新客户的serller id 把旧的id替换成新的id  更新当前客户的市场人员
            customerMapper.updateSellerId(entity.getNewSeller().getId(),entity.getCustomer().getId());

            //应该把customer的状态status=潜在客户 不然还在客户池中 将当前客户的状态改为潜在状态
            //客户池中的客户先更改为潜在客户  潜在客户中的客户做移交 不需要转为潜在客户
            customerMapper.updateStatus(entity.getCustomer().getId(), Customer.STATUS_POTENTIAL);

    }

    @Override
    public PageInfo<CustomerTransfer> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"tf.operate_time DESC");
        List<CustomerTransfer> list = CustomerTransferMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

}
